package com.qiguliuxing.dts.admin.controller;

import com.github.pagehelper.PageInfo;
import com.qiguliuxing.dts.admin.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;
import com.qiguliuxing.dts.db.domain.DtsBankSupport;
import com.qiguliuxing.dts.service.base.DtsBankSupportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/admin/bank")
@Api(tags = ApiAdminConsts.SupportBank.PARENT)
@Slf4j
public class AdminSupportBankController {

	@Autowired
	private DtsBankSupportService bankSupportService;

    @GetMapping("/list")
	@RequiresPermissions("admin:bank:list")
	@RequiresPermissionsDesc(menu = { "系统管理", "支付行管理" }, button = "查询")
    @ApiOperation(value = ApiAdminConsts.SupportBank.LIST)
    @WebLog(description = ApiAdminConsts.SupportBank.LIST, excludes = {LogParamsConsts.RESPONSE})
	public Object list(@ApiParam(value = "名称") String name,
					   @ApiParam(value = "编码") String code,
					   @ApiParam(value = "页码",defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
					   @ApiParam(value = "分页大小",defaultValue = "10") @RequestParam(defaultValue = "10") Integer limit,
					   @ApiParam(value = "排序字段",defaultValue = "add_time") @Sort @RequestParam(defaultValue = "add_time") String sort,
					   @ApiParam(value = "排序规则",defaultValue = "desc") @Order @RequestParam(defaultValue = "desc") String order) {
		List<DtsBankSupport> adList = bankSupportService.querySelective(name, code, page, limit, sort, order);
		long total = PageInfo.of(adList).getTotal();
		Map<String, Object> data = new HashMap<>();
		data.put("total", total);
		data.put("items", adList);
		return ResponseUtils.ok(data);
	}

	private Object validate(DtsBankSupport bank) {
		if (StringUtils.isEmpty(bank.getBankName())) {
			return ResponseUtils.badArgument();
		}
		if (StringUtils.isEmpty(bank.getBankCode())) {
			return ResponseUtils.badArgument();
		}
		return null;
	}

	@PostMapping("/create")
	@RequiresPermissions("admin:bank:create")
	@RequiresPermissionsDesc(menu = { "系统管理", "支付行管理" }, button = "添加")
	@ApiOperation(value = ApiAdminConsts.SupportBank.CREATE)
    @WebLog(description = ApiAdminConsts.SupportBank.CREATE, persistence = true)
	public Object create(@RequestBody DtsBankSupport bank) {
		Object error = validate(bank);
		if (error != null) {
			log.error("支付行添加校验不通过!");
			return error;
		}
		bankSupportService.add(bank);
		return ResponseUtils.ok(bank);
	}

	@PostMapping("/update")
	@RequiresPermissions("admin:bank:update")
	@RequiresPermissionsDesc(menu = { "系统管理", "支付行管理" }, button = "编辑")
	@ApiOperation(value = ApiAdminConsts.SupportBank.UPDATE)
    @WebLog(description = ApiAdminConsts.SupportBank.UPDATE, persistence = true)
	public Object update(@RequestBody DtsBankSupport bank) {
		Object error = validate(bank);
		if (error != null) {
			return error;
		}
		if (bankSupportService.updateById(bank) == 0) {
			log.info("推广管理->广告管理->编辑,更新广告数据失败!");
			return ResponseUtils.updatedDataFailed();
		}
		return ResponseUtils.ok(bank);
	}

	@PostMapping("/delete")
	@RequiresPermissions("admin:bank:delete")
	@RequiresPermissionsDesc(menu = { "系统管理", "支付行管理" }, button = "删除")
	@ApiOperation(value = ApiAdminConsts.SupportBank.DELETE)
    @WebLog(description = ApiAdminConsts.SupportBank.DELETE, persistence = true)
	public Object delete(@RequestBody DtsBankSupport bank) {
		Integer id = bank.getId();
		if (id == null) {
			return ResponseUtils.badArgument();
		}
		bankSupportService.deleteById(id);
		return ResponseUtils.ok();
	}

}
